function debounce(fn, delay = 1000, immediate) {
  let timer = null;
  let first = 1;

  function _debounce(...args) {
    if (timer) clearTimeout(timer);
    if (immediate) {
      first = !timer;
      if (first) fn.apply(this, args);
      timer = setTimeout(() => {
        timer = null;
      }, delay);
    } else {
      timer = setTimeout(() => {
        fn.apply(this, args);
      }, timer);
    }
  }

  return _debounce;
}

function throttle(fn, delay = 1000) {
  let lastTime = 0;
  return (_throttle = function (...args) {
    let nowTime = +new Date();
    let remain = nowTime - lastTime;
    if (remain >= delay) {
      fn.apply(this, args);
      lastTime = nowTime;
    }
  });
}
